﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UserAccountDao
/// </summary>
public class UserAccountDao
{
    #region "variable"
    private string _error;
    private MyConnection _myConn;
    #endregion


    #region "properties"
    public string Error
    {
        get { return _error; }
        set { _error = value; }
    }

    #endregion
	public UserAccountDao()
	{
		_error = "";
        _myConn = new MyConnection();
	}
    public void DeleteUserAccount(int id_UA)
    {
        string spName = "sp_Delete_UserAccount";
        SqlParameter[] parameter = {new SqlParameter("@id_UA",SqlDbType.Int)};
        parameter[0].Value = id_UA;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public void UpdateUserAccount(UserAccountDto _userAccountDto)
    {
        string spName = "sp_Update_UserAccount";
        SqlParameter[] parameter = {new SqlParameter("@id_UA",SqlDbType.Int),
                                    new SqlParameter("@_fullName",SqlDbType.NVarChar,30),
                                    new SqlParameter("@_birthday",SqlDbType.NVarChar,30),
                                    new SqlParameter("@_sex",SqlDbType.Bit),
                                    new SqlParameter("@_email",SqlDbType.NVarChar,50),
                                    new SqlParameter("@_adress",SqlDbType.NVarChar,50)};
        parameter[0].Value = _userAccountDto.Id_UA;
        parameter[1].Value = _userAccountDto.Name;
        parameter[2].Value = _userAccountDto.DateOfBirth;
        parameter[3].Value = _userAccountDto.Sex;
        parameter[4].Value = _userAccountDto.Email;
        parameter[5].Value = _userAccountDto.Adress;
        _myConn.ExecuteNonQuery(spName, parameter);
    }
    public List<UserAccountDto> SelectUserAccountBy(string _searchColumn,string _searchString)
    {
        string spName = "sp_Select_UserAccountBy";
        SqlParameter[] parameter = {new SqlParameter("@_searchColumn",SqlDbType.NVarChar,20),
                                    new SqlParameter("@_searchString",SqlDbType.NVarChar,20)};
        parameter[0].Value = _searchColumn;
        parameter[1].Value = _searchString;
        DataTable table = _myConn.GetDataTable(spName, parameter);
        return this.MakeListBillDto(table);
    }

    public List<UserAccountDto> MakeListBillDto(DataTable table)
    {
        List<UserAccountDto> list = new List<UserAccountDto>();
        foreach (DataRow row in table.Rows)
        {
            list.Add(new UserAccountDto(row));
        }
        return list;
    }
}